//
//  CZVistorView.swift
//  Swift -- 微博
//
//  Created by sks on 16/6/26.
//  Copyright © 2016年 mohaha. All rights reserved.
//

import UIKit
import SnapKit

class CZVistorView: UIView {

    //button按钮点击闭包
    var closure :(()->())?
    
  
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setUpUI() {
    
        backgroundColor = RBG(236, green: 236, blue: 236)
        
        addSubview(backImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(logInBtn)
        addSubview(registBtn)
        
        //添加约束
        //圆圈
        backImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //遮罩
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //图标
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //文字
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
        }
        //登录
        logInBtn.snp_makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(36)
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        //注册
        registBtn.snp_makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(36)
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
    
    }
    
    
    //懒加载View上的视图控件
    //圆圈
    private lazy var backImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var maskImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //房子图标
    private lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    private lazy var messageLabel : UILabel = {
    
        let label = UILabel()
        label.textColor = UIColor.orangeColor()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        return label
    
    }()
    
    //注册按钮
    lazy var registBtn :UIButton = {
    
        let btn = UIButton()
        btn .addTarget(self, action: #selector(CZVistorView.btnClick), forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return btn
    
    }()
    
    //登录按钮
    lazy var logInBtn : UIButton = {
       let btn = UIButton()
        btn .addTarget(self, action: #selector(CZVistorView.btnClick), forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return btn
    
    }()
    
    //按钮点击事件
    @objc private func btnClick(){
    
      closure!()
    }
    
    
    //圆圈动画
    func setupAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 10 //动画时间
        anim.toValue = M_PI * 2 //旋转度数
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false //
        
        backImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    //设置每个界面的初始信息
    func setupVisitorInfo(imageName:String?,title:String?)  {
        
        //如果有值就代表不是主页 设置其属性
        if let img = imageName, let tit = title {
            iconImageView.image = UIImage (named: img)
            messageLabel.text = tit
            backImageView.hidden = true
            
        }else {
        
           setupAnim()
        }
    }
    

}
